package com.example.event_resource_java.authority.controller.StatisticController;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.event_resource_java.authority.server.StatisticService;
import com.example.event_resource_java.authority.server.UserServer;
import com.example.event_resource_java.commons.domain.UserDO;
import com.example.event_resource_java.commons.model.LearnTimeBody;
import com.example.event_resource_java.commons.util.ApiResult;
import com.example.event_resource_java.commons.util.JWTUtil;
import io.swagger.annotations.Api;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName CourseLearnController
 * @Description: 课程学习统计
 * @Author: ak
 * @CreateDate: 2024/4/20 10:18
 * @UpdateUser:
 * @UpdateDate: 2024/4/20 10:18
 * @UpdateRemark:
 * @Version: 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/statistic")
public class StatisticController {

    @Autowired
    private StatisticService statisticService;

    @Autowired
    private UserServer userServer;

    /**
     * @auther: ak
     * @Description 老师查看所有班级学生练习记录
     * @param:
     * @return:
     * @date: 2024/4/22 9:55
     */
    @GetMapping("/list")
    public ApiResult list(@RequestParam("studentName") String studentName, @RequestParam("className") String className,
                          @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return ApiResult.success(statisticService.list(className, studentName, pageSize, pageNum));
    }


    /**
     * @auther: ak
     * @Description 学生练习记录详情
     * @param:
     * @return:
     * @date: 2024/4/22 9:56
     */
    @GetMapping("/practiceRecordDetails")
    public ApiResult practiceRecordDetails(@RequestParam("userId") Long userId,
                                           @RequestParam("pageNum") Integer pageNum,
                                           @RequestParam("pageSize") Integer pageSize) {
        return ApiResult.success(statisticService.practiceRecordDetails(userId, pageNum, pageSize));
    }

    /**
     * @auther: ak
     * @Description 查看学生练习试卷
     * @param:
     * @return:
     * @date: 2024/4/30 8:49
     */
    @GetMapping("/getExamPaper")
    public ApiResult getExamPaper(Long practiceRecordId, Long userId) {
        return ApiResult.success(statisticService.getExamPaper(practiceRecordId, userId));
    }


    /**
     * @auther: ak
     * @Description 老师班级学习统计
     * @param:
     * @return:
     * @date: 2024/4/22 9:57
     */
    @GetMapping("/listLearnStatistic")
    public ApiResult listLearnStatistic(@RequestParam("classname") String classname,
                                        @RequestParam("studentName") String StudentName,
                                        @RequestParam("pageNum") Integer pageNum,
                                        @RequestParam("pageSize") Integer pageSize) {
        return ApiResult.success(statisticService.listLearnStatistic(classname, StudentName, pageNum, pageSize));
    }

    /**
     * @auther: ak
     * @Description 学生学习详情
     * @param:
     * @return:
     * @date: 2024/4/22 9:58
     */
    @GetMapping("/learnDetails")
    public ApiResult learnDetails(@RequestParam("userId") Long userId,
                                  @RequestParam("type") Integer type,
                                  @RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize) {
        return ApiResult.success(statisticService.learnDetails(userId, type, pageNum, pageSize));
    }

    /**
     * @auther: ak
     * @Description 插入/更新 视频学习时长统计 和 PDF点击观看
     * @param:
     * @return:
     * @date: 2024/4/22 11:18
     */
    @PostMapping("/learnTime")
    public ApiResult learnTime(@RequestBody LearnTimeBody learnTimeBody) {
        Long userId = JWTUtil.getUser().getId();
        QueryWrapper<UserDO> userDOQueryWrapper = new QueryWrapper<>();
        userDOQueryWrapper.eq("id", userId);
        UserDO one = userServer.getOne(userDOQueryWrapper);
        if (one.getType() == 1) {
            return ApiResult.success(true);
        }

        return ApiResult.success(statisticService.saveOrUpdateLearnTime(learnTimeBody));
    }

    /**
     * @auther: ak
     * @Description 视频重新播放
     * @param:
     * @return:
     * @date: 2024/4/22 11:18
     */
    @GetMapping("/backup")
    public ApiResult backup(@RequestParam("allocationsId") Long allocationsId) {

        return ApiResult.success(statisticService.backup(allocationsId));
    }

}
